package com.ops.controller;

import com.ops.entity.Hospital;
import com.ops.service.HospitalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 医院管理控制器
 * 处理医院信息的导入、查询、添加和删除等操作
 */
@Controller
@RequestMapping("/hospitals")
public class HospitalController {

    @Autowired
    private HospitalService hospitalService;

    /**
     * 显示医院数据导入页面
     */
    @GetMapping("/hospital-import")
    public String showImportPage() {
        return "hospital-import";
    }

    /**
     * 处理医院数据Excel导入
     * 支持批量导入医院信息
     * 
     * @param file Excel文件
     * @return 导入结果
     */
    @PostMapping("/import")
    @ResponseBody
    public ResponseEntity<?> importHospitals(@RequestParam("file") MultipartFile file) {
        try {
            hospitalService.importHospitals(file);
            Map<String, Object> response = new HashMap<>();
            response.put("message", "医院数据导入成功");
            response.put("success", true);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("error", "导入失败：" + e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    /**
     * 获取所有医院列表
     */
    @GetMapping("/list")
    @ResponseBody
    public ResponseEntity<List<Hospital>> getHospitalList() {
        return ResponseEntity.ok(hospitalService.getAllHospitals());
    }

    /**
     * 添加新医院
     * 
     * @param request 包含医院信息的请求体
     * @return 添加结果
     */
    @PostMapping("/add")
    public ResponseEntity<?> addHospital(@RequestBody Map<String, String> request) {
        Map<String, Object> response = new HashMap<>();
        try {
            String hospitalName = request.get("hospitalName");
            String hospitalId = request.get("hospitalId");
            
            // 参数验证
            if (hospitalName == null || hospitalName.trim().isEmpty() || 
                hospitalId == null || hospitalId.trim().isEmpty()) {
                response.put("success", false);
                response.put("message", "医院名称和ID不能为空");
                return ResponseEntity.ok(response);
            }

            // 检查重复
            if (hospitalService.existsByHospitalId(hospitalId)) {
                response.put("success", false);
                response.put("message", "医院ID已存在");
                return ResponseEntity.ok(response);
            }

            // 创建并保存医院信息
            Hospital hospital = new Hospital();
            hospital.setHospitalName(hospitalName);
            hospital.setHospitalId(hospitalId);
            hospitalService.save(hospital);

            response.put("success", true);
            response.put("message", "医院添加成功");
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "添加医院失败: " + e.getMessage());
        }
        return ResponseEntity.ok(response);
    }

    /**
     * 删除医院
     * 
     * @param hospitalId 医院ID
     * @return 删除结果
     */
    @DeleteMapping("/delete/{hospitalId}")
    public ResponseEntity<?> deleteHospital(@PathVariable String hospitalId) {
        Map<String, Object> response = new HashMap<>();
        try {
            if (!hospitalService.existsByHospitalId(hospitalId)) {
                response.put("success", false);
                response.put("message", "医院不存在");
                return ResponseEntity.ok(response);
            }

            hospitalService.deleteByHospitalId(hospitalId);
            response.put("success", true);
            response.put("message", "医院删除成功");
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "删除医院失败: " + e.getMessage());
        }
        return ResponseEntity.ok(response);
    }
} 